package com.ruoyi.system.repository.service.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.ezmorph.MorpherRegistry;
import net.sf.json.JSONArray;
import com.ruoyi.system.repository.domain.*;
import com.ruoyi.system.repository.mapper.OrdertableMapper;
import com.ruoyi.system.repository.service.IOrdertableService;
import org.apache.commons.beanutils.BasicDynaBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 订单Service业务层处理
 *
 * @author qiang
 * @date 2021-01-01
 */
@Service
public class OrdertableServiceImpl implements IOrdertableService {
    @Autowired
    private OrdertableMapper ordertableMapper;

    /**
     * 查询订单
     *
     * @param orderid 订单ID
     * @return 订单
     */
    @Override
    public Ordertable selectOrdertableById(String orderid) {
        return ordertableMapper.selectOrdertableById(orderid);
    }

    /**
     * 查询订单列表
     *
     * @param ordertable 订单
     * @return 订单
     */
    @Override
    public List<Ordertable> selectOrdertableList(Ordertable ordertable)  {
//        Date date = ordertable.getOrdertime();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        String s=sdf.format(date);
//        try {
//            date=sdf.parse(s);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        ordertable.setOrdertime(date);
        return ordertableMapper.selectOrdertableList(ordertable);
    }

    /**
     * 新增订单
     *
     * @param ordertable 订单
     * @return 结果
     */
    @Override
    public int insertOrdertable(Ordertable ordertable) {
        return ordertableMapper.insertOrdertable(ordertable);
    }

    /**
     * 修改订单
     *
     * @param ordertable 订单
     * @return 结果
     */
    @Override
    public int updateOrdertable(Ordertable ordertable) {
                                                                                                                                                                                                    return ordertableMapper.updateOrdertable(ordertable);
    }

    /**
     * 批量删除订单
     *
     * @param orderids 需要删除的订单ID
     * @return 结果
     */
    @Override
    public int deleteOrdertableByIds(String[] orderids) {
        return ordertableMapper.deleteOrdertableByIds(orderids);
    }

    /**
     * 删除订单信息
     *
     * @param orderid 订单ID
     * @return 结果
     */
    @Override
    public int deleteOrdertableById(String orderid) {
        return ordertableMapper.deleteOrdertableById(orderid);
    }


    @Override
    public List<Order> findAllOrderByName(String userName){
        List<OrderEntry> entryList=ordertableMapper.findAllOrderByName(userName);
        List<Order> orderList=new ArrayList<>();
        List<OrderDrink> drinkList=new ArrayList<>();
        Order orderTmp=new Order();
        OrderDrink drinkTmp=new OrderDrink();
        int orderId=-1;
        double totalPrice=0;
        for(OrderEntry e:entryList){
            //当前条目属于一个新订单
            if(orderId!=e.getOrderId()){
                //不是第一个条目才需要添加orderList
                if(orderId!=-1){
                    orderTmp.setTotalPrice(totalPrice);
                    orderTmp.setDrinkList(drinkList);
                    orderList.add(orderTmp);
                }
                //清空上个订单用到的变量
                orderTmp=new Order();
                drinkTmp=new OrderDrink();
                totalPrice=0;
                drinkList=new ArrayList<>();

                orderId=e.getOrderId();
                orderTmp.setOrderId(e.getOrderId());
                orderTmp.setOrderTime(e.getOrderTime());
                orderTmp.setPickId(e.getPickId());
                orderTmp.setUserName(e.getUserName());

                //加入一种饮品
                drinkTmp.setDrinkId(e.getDrinkId());
                drinkTmp.setDrinkInfo(e.getDrinkInfo());
                drinkTmp.setDrinkName(e.getDrinkName());
                drinkTmp.setDrinkNum(e.getDrinkNum());
                drinkTmp.setDrinkPrice(e.getDrinkPrice());
                totalPrice+=e.getDrinkPrice()*e.getDrinkNum();
                drinkList.add(drinkTmp);

            }else{
                totalPrice+=e.getDrinkPrice()*e.getDrinkNum();
                drinkTmp=new OrderDrink();
                drinkTmp.setDrinkId(e.getDrinkId());
                drinkTmp.setDrinkInfo(e.getDrinkInfo());
                drinkTmp.setDrinkName(e.getDrinkName());
                drinkTmp.setDrinkNum(e.getDrinkNum());
                drinkTmp.setDrinkPrice(e.getDrinkPrice());
                drinkList.add(drinkTmp);
            }
        }

        orderTmp.setTotalPrice(totalPrice);
        orderTmp.setDrinkList(drinkList);
        orderList.add(orderTmp);
        return orderList;
    }
    @Override
    public List<MiniOrder> findAllMiniOrder(String userName){
        List<MiniOrderEntry> entryList=ordertableMapper.findAllMiniOrder(userName);
        List<MiniOrder> orderList=new ArrayList<>();
        List<String> drinkIdList=new ArrayList<>();
        MiniOrder orderTmp=new MiniOrder();
        String drinkId;
        int orderId=-1;
        double totalPrice=0;
        //查询结果为空
        if(entryList.size()==0){
            return null;
        }
        for(MiniOrderEntry e:entryList){
            //当前条目属于一个新订单
            if(orderId!=e.getOrderId()){
                //不是第一个条目才需要添加orderList
                if(orderId!=-1){
                    orderTmp.setTotalPrice(totalPrice);
                    orderTmp.setDrinkIdList(drinkIdList);
                    orderList.add(orderTmp);
                }
                //清空上个订单用到的变量
                orderTmp=new MiniOrder();
                drinkIdList=new ArrayList<>();
                totalPrice=0.0;
                //不是第一个条目需要清空drinkList
                if(orderId!=-1){
                    drinkIdList.clear();
                }


                orderId=e.getOrderId();
                orderTmp.setOrderId(e.getOrderId());
                orderTmp.setOrderTime(e.getOrderTime());
                orderTmp.setUserName(e.getUserName());

                //加入一种饮品
                drinkIdList.add(e.getDrinkId());
                totalPrice+=e.getDrinkPrice()*e.getDrinkNum();

            }else{
                totalPrice+=e.getDrinkPrice()*e.getDrinkNum();
                drinkIdList.add(e.getDrinkId());
            }
        }

        orderTmp.setTotalPrice(totalPrice);
        orderTmp.setDrinkIdList(drinkIdList);
        orderList.add(orderTmp);
        return orderList;
    }
    @Override
    public List<MiniOrder> findTodayMiniOrder(String userName) {
        List<MiniOrderEntry> entryList=ordertableMapper.findTodayMiniOrder(userName);
        List<MiniOrder> orderList=new ArrayList<>();
        //List<OrderDrink> drinkList=new ArrayList<>();
        List<String> drinkIdList=new ArrayList<>();
        MiniOrder orderTmp=new MiniOrder();
        //OrderDrink drinkTmp=new OrderDrink();
        String drinkId;
        int orderId=-1;
        double totalPrice=0;
        //查询结果为空
        if(entryList.size()==0)
            return null;
        for(MiniOrderEntry e:entryList){
            //当前条目属于一个新订单
            if(orderId!=e.getOrderId()){
                //不是第一个条目才需要添加orderList
                if(orderId!=-1){
                    orderTmp.setTotalPrice(totalPrice);
                    orderTmp.setDrinkIdList(drinkIdList);
                    orderList.add(orderTmp);
                }
                //清空上个订单用到的变量
                orderTmp=new MiniOrder();
                totalPrice=0;
                drinkIdList=new ArrayList<>();
                if(orderId!=-1){
                    drinkIdList.clear();
                }


                orderId=e.getOrderId();
                orderTmp.setOrderId(e.getOrderId());
                orderTmp.setOrderTime(e.getOrderTime());
//                orderTmp.setPickId(e.getPickId());
                orderTmp.setUserName(e.getUserName());

                //加入一种饮品
                drinkIdList.add(e.getDrinkId());
                totalPrice+=e.getDrinkPrice()*e.getDrinkNum();


            }else{
                drinkIdList.add(e.getDrinkId());
                totalPrice+=e.getDrinkPrice()*e.getDrinkNum();
            }
        }

        orderTmp.setTotalPrice(totalPrice);
        orderTmp.setDrinkIdList(drinkIdList);
        orderList.add(orderTmp);
        return orderList;
    }
    @Override
    public Order findOneOrder(String userName, int orderId){
        List<OrderEntry> orderEntryList=ordertableMapper.findOneOrder(userName,orderId);
        Order order=new Order();
        List<OrderDrink> drinkList=new ArrayList<>();
        OrderDrink drinkTmp=new OrderDrink();
        double totalPrice=0;
        int i=0;
        for(OrderEntry e:orderEntryList){
            if(i==0){
                order.setOrderId(e.getOrderId());
                order.setUserName(e.getUserName());
                order.setPickId(e.getPickId());
                order.setOrderTime(e.getOrderTime());
                i++;
            }
            //添加饮料和计算总金额
            drinkTmp = new OrderDrink();
            drinkTmp.setDrinkId(e.getDrinkId());
            drinkTmp.setDrinkInfo(e.getDrinkInfo());
            drinkTmp.setDrinkName(e.getDrinkName());
            drinkTmp.setDrinkNum(e.getDrinkNum());
            drinkTmp.setDrinkPrice(e.getDrinkPrice());
            totalPrice+=e.getDrinkPrice()*e.getDrinkNum();
            drinkList.add(drinkTmp);
        }
        order.setDrinkList(drinkList);
        order.setTotalPrice(totalPrice);
        return order;
    }
    @Override
    public boolean addOneOrder(Order order){
        //加入时间、订单编号以及取茶号
        Timestamp time=new Timestamp(System.currentTimeMillis());
        int orderId=ordertableMapper.findLastOrderId();
        int pickId=ordertableMapper.findLastPickId();
        //订单条目
        OrderEntry orderEntry;
        List<OrderDrink> drinkList=order.getDrinkList();
        for(OrderDrink d:drinkList){
            orderEntry=new OrderEntry();
            orderEntry.setUserName(order.getUserName());
            orderEntry.setDrinkId(d.getDrinkId());
            orderEntry.setDrinkInfo(d.getDrinkInfo());
            orderEntry.setDrinkName(d.getDrinkName());
            orderEntry.setDrinkNum(d.getDrinkNum());
            orderEntry.setDrinkPrice(d.getDrinkPrice());
            orderEntry.setOrderId(orderId);
            orderEntry.setOrderTime(time);
            orderEntry.setPickId(pickId);
            if(ordertableMapper.addOneOrder(orderEntry)==0){
                return false;
            }
        }
        return true;
    }
    @Override
    public boolean addOneOrderByStr(String userName,String drinkStr){
        JSONArray json= JSONArray.fromObject(drinkStr);
        List<OrderDrink> drinkList=(List<OrderDrink>)JSONArray.toCollection(json,OrderDrink.class);
        //加入时间、订单编号以及取茶号
        Timestamp time=new Timestamp(System.currentTimeMillis()); ;
        int orderId=ordertableMapper.findLastOrderId()+1;
        int pickId=ordertableMapper.findLastPickId()+1;

        OrderEntry orderEntry;

        //拆分drinklist,组成为单条条目，插入数据库
        for(OrderDrink d:drinkList)
        {
            orderEntry=new OrderEntry();
            orderEntry.setUserName(userName);
            orderEntry.setDrinkId(d.getDrinkId());
            orderEntry.setDrinkInfo(d.getDrinkInfo());
            orderEntry.setDrinkName(d.getDrinkName());
            orderEntry.setDrinkNum(d.getDrinkNum());
            orderEntry.setDrinkPrice(d.getDrinkPrice());
            orderEntry.setOrderId(orderId);
            orderEntry.setOrderTime(time);
            orderEntry.setPickId(pickId);
            if(ordertableMapper.addOneOrder(orderEntry)==0)
                return false;
        }
        return true;
    }
}
